package com.xyazm.wms.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xyazm.common.utils.RandomUtil;
import com.xyazm.wms.dao.DeliveryOrderDAO;
import com.xyazm.wms.dao.DeliveryOrderItemDAO;
import com.xyazm.wms.domain.dto.ScheduleDeliveryResultDTO;
import com.xyazm.wms.domain.entity.DeliveryOrderDO;
import com.xyazm.wms.domain.entity.DeliveryOrderItemDO;
import com.xyazm.wms.domain.param.PickGoodsParam;
import com.xyazm.wms.enums.DeliveryOrderStatusEnum;
import com.xyazm.wms.exception.WmsBizException;
import com.xyazm.wms.mapstruct.WmsApiMapstruct;
import com.xyazm.wms.service.IWmsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class WmsServiceImpl implements IWmsService {

    @Autowired
    private DeliveryOrderDAO deliveryOrderDAO;

    @Autowired
    private DeliveryOrderItemDAO deliveryOrderItemDAO;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void pickGoods(PickGoodsParam param) {
        log.info("捡货,orderId={},request={}", param.getOrderId(), JSONObject.toJSONString(param));
        String wmsException = param.getWmsException();

        if(StringUtils.isNotBlank(wmsException) && wmsException.equals("true")) {
            throw new WmsBizException("捡货异常！");
        }

        // 1.捡货,调度出库
        ScheduleDeliveryResultDTO result = scheduleDelivery(param);

        // 2.存储出库单和出库单条目
        deliveryOrderDAO.save(result.getDeliveryOrderDO());
        deliveryOrderItemDAO.saveBatch(result.getDeliveryOrderItemDOList());
    }

    @Override
    public void cancelPickGoods(String orderId) {
        log.info("取消捡货,orderId={}", orderId);
        // 1.查询出库单
        List<DeliveryOrderDO> deliveryOrderDOS = deliveryOrderDAO.listByOrderId(orderId);

        // 2.移除出库单和条目
        if(CollectionUtils.isNotEmpty(deliveryOrderDOS)) {
            deliveryOrderDAO.cancelPickGoods(orderId, DeliveryOrderStatusEnum.CANCEL.getCode());
            List<String> deliveryOrderIds = deliveryOrderDOS.stream()
                    .map(DeliveryOrderDO::getDeliveryOrderId).collect(Collectors.toList());
            deliveryOrderItemDAO.cancelPickGoodsItem(deliveryOrderIds,DeliveryOrderStatusEnum.CANCEL.getCode());
        }
    }



    /**
     * 捡货，调度出库
     * @author xuyou
     * 2022/7/31 0031 22:19
    */
    private ScheduleDeliveryResultDTO scheduleDelivery(PickGoodsParam param) {
        log.info("orderId={}的订单进行调度出库", param.getOrderId());
        // 1.生成出库单ID
        String deliveryOrderId = genDeliveryOrderId();

        // 2.生成出库单
        DeliveryOrderDO deliveryOrderDO = WmsApiMapstruct.INSTANCE.convertDeliveryOrder(param);
        deliveryOrderDO.setDeliveryOrderId(deliveryOrderId);

        // 3.生成出库单条目
        List<DeliveryOrderItemDO> deliveryOrderItemDOS = WmsApiMapstruct.INSTANCE.convertDeliveryOrderItem(param.getOrderItems());
        for(DeliveryOrderItemDO item : deliveryOrderItemDOS) {
            // 设置出库单id
            item.setDeliveryOrderId(deliveryOrderId);
            // 这里仅仅只是模拟，假设有一个无限货物的仓库货柜(id = 1)
            item.setPickingCount(item.getSaleQuantity());
            item.setSkuContainerId(1);
        }
        return new ScheduleDeliveryResultDTO(deliveryOrderDO, deliveryOrderItemDOS);
    }

    /**
     * 生成出库单id
     * @return
     */
    private String genDeliveryOrderId() {
        return RandomUtil.genRandomNumber(10);
    }



}
